package com.dashu.core.controller;

import com.dashu.base.enums.ReplRyCode;
import com.dashu.core.dto.PageDTO;
import com.dashu.core.result.PageEnty;
import com.dashu.core.result.ResultBean;
import com.dashu.core.dto.company.CompanyDepartmentDTO;
import com.dashu.core.service.CompanyDepartmentService;
import com.dashu.core.vo.company.CompanyDepartmentVO;
import com.dashu.core.vo.UpdateEntityVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;


/**
 * @Author xys
 * @Date 2020/ReplRyCode.SUCCESS.getCode()ReplRyCode.SUCCESS.getCode()/ReplRyCode.SUCCESS.getCode()7 ReplRyCode.SUCCESS.getCode()5:2ReplRyCode.SUCCESS.getCode()
 */

@RequestMapping("dashu/companyDepartment")
@RestController
@Slf4j
@Api(value = "dashu/companyDepartment", tags = {"单位部门模块"}, description = "后台")
public class CompanyDepartmentController {

    @Autowired
    private CompanyDepartmentService companyDepartmentService;
//    @Autowired
//    RestTemplate restTemplate;

    @PostMapping("/addDepartment")
    @ApiOperation(value = "部门添加", notes = "部门添加")
    public ResultBean<CompanyDepartmentDTO> addDepartment(@RequestBody @Validated CompanyDepartmentVO companyDepartmentVO) {
        CompanyDepartmentDTO companyDepartmentDTO = companyDepartmentService.existByCompanyDepartmentName(companyDepartmentVO.getGradeName(),companyDepartmentVO.getCompanyId());
        if (companyDepartmentDTO != null) {
            return new ResultBean<>(ReplRyCode.EXIST_COMPANY_DEPARTMENT.getCode(), ReplRyCode.EXIST_COMPANY_DEPARTMENT.getMessage(), null);
        }
        if (companyDepartmentVO == null) {
            return new ResultBean<>(ReplRyCode.DATA_NOT_NULL.getCode(), ReplRyCode.DATA_NOT_NULL.getMessage(), null);
        }
        return new ResultBean<>(ReplRyCode.SUCCESS.getCode(), "部门添加", companyDepartmentService.add(companyDepartmentVO));
    }

    @PostMapping("/updataDepartment")
    @ApiOperation(value = "部门信息修改", notes = "部门信息修改")
    public ResultBean<CompanyDepartmentDTO> updataDepartment(@RequestBody @Validated UpdateEntityVO<CompanyDepartmentVO> updateEntityVO) throws Exception {
        if (updateEntityVO == null) {
            return new ResultBean<>(ReplRyCode.DATA_NOT_NULL.getCode(), ReplRyCode.DATA_NOT_NULL.getMessage(), null);
        }
        return new ResultBean<>(ReplRyCode.SUCCESS.getCode(), "部门信息修改", companyDepartmentService.updataByCompanyDepartmentId(updateEntityVO));
    }

    @PostMapping("/findBase")
    @ApiOperation(value = "父节点查询部门列表", notes = "父节点查询部门列表")
    public ResultBean<List<CompanyDepartmentDTO>> findBase(@RequestBody @Validated PageEnty<Long> creteUserVo) {
        if (creteUserVo == null) {
            return new ResultBean<>(ReplRyCode.DATA_NOT_NULL.getCode(), ReplRyCode.DATA_NOT_NULL.getMessage(), null);
        }
        return new ResultBean<>(ReplRyCode.SUCCESS.getCode(), "父节点查询部门列表", companyDepartmentService.findByCreteUser(creteUserVo));
    }

    @GetMapping("/findDepartment")
    @ApiOperation(value = "部门信息查询", notes = "部门信息查询")
    public ResultBean<CompanyDepartmentDTO> findDepartment(long companyDepartmentId) {
        if (companyDepartmentId == 0) {
            return new ResultBean<>(ReplRyCode.DATA_NOT_NULL.getCode(), ReplRyCode.DATA_NOT_NULL.getMessage(), null);
        }
        return new ResultBean<>(ReplRyCode.SUCCESS.getCode(), "部门信息查询", companyDepartmentService.findByCompanyDepartmentId(companyDepartmentId));
    }

    @DeleteMapping("/deleteDepartment")
    @ApiOperation(value = "部门信息删除", notes = "部门信息删除")
    public ResultBean<Boolean> deleteDepartment(long companyDepartmentId) {
        if (companyDepartmentId == 0) {
            return new ResultBean<>(ReplRyCode.DATA_NOT_NULL.getCode(), ReplRyCode.DATA_NOT_NULL.getMessage(), null);
        }
        return new ResultBean<>(ReplRyCode.SUCCESS.getCode(), "部门信息删除", companyDepartmentService.deleteByCompanyDepartmentId(companyDepartmentId));
    }

    @PostMapping("/pageDepartmentList")
    @ApiOperation(value = "部门信息列表", notes = "部门信息列表")
    public ResultBean<PageDTO<CompanyDepartmentDTO>> pageDepartmentList(@RequestBody @Validated PageEnty<CompanyDepartmentVO> departmentListPageEntyVO) throws Exception {
        PageDTO<CompanyDepartmentDTO> pageDTO = new PageDTO<>();
        if (departmentListPageEntyVO == null) {
            return new ResultBean<>(ReplRyCode.DATA_NOT_NULL.getCode(), ReplRyCode.DATA_NOT_NULL.getMessage(), null);
        }
        long total = companyDepartmentService.departmentCount(departmentListPageEntyVO);
        pageDTO.setTotal(total);
        pageDTO.setDataList(companyDepartmentService.pageDepartmentList(departmentListPageEntyVO));
        return new ResultBean<>(ReplRyCode.SUCCESS.getCode(), "部门信息列表",pageDTO );
    }
}
